/* 
 * 
 * Copyright (c) 2007 John Wells
 * Copyright (c) 2010 Photonic Games

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace GameLevel.ContentPipeline.Leadwerks14
{
    public sealed class Brush
    {
        #region Fields
        // Public fields
        public BrushFlags Flags;
        public KeyValuePair<string, string>[] Items;
        public int GroupIndex, VisibilityGroupIndex;
        public Color Color;
        public Vector3[] Vertices;
        public List<BrushFace> VisibleFaces = new List<BrushFace>();
        public List<BrushFace> AllFaces = new List<BrushFace>();
        public bool isTrigger;
        public bool isModel;
        public String Name;

        // Enumerations
        [Flags]
        public enum BrushFlags
        {
            None = 0
        }
        #endregion

        public Brush(BinaryReader reader, string[] names)
        {
            // Read the brush flags
            this.Flags = (BrushFlags)reader.ReadByte();

            // Read the items array
            this.Items = new KeyValuePair<string, string>[reader.ReadInt32()];
            for (int index = 0; index < this.Items.Length; index++)
            {
                // Entity item keys and values are just strings...
                string key = names[reader.ReadInt32() - 1];
                string value = names[reader.ReadInt32() - 1];

                // Set this entity item
                this.Items[index] = new KeyValuePair<string, string>(key, value);
            }

            // Read group information
            this.GroupIndex = reader.ReadInt32();
            this.VisibilityGroupIndex = reader.ReadInt32();

            // Read the brush color
            this.Color = Leadwerks14Importer.ReadColor(reader);

            // Read the array of position vertices
            this.Vertices = new Vector3[reader.ReadByte()];
            for (int index = 0; index < this.Vertices.Length; index++)
                this.Vertices[index] = Leadwerks14Importer.ReadVector3(reader);

            //System.Diagnostics.Debugger.Launch();

            // Read the array of faces
            byte faceCount = reader.ReadByte();
            for (int index = 0; index < faceCount; index++)
            {
                BrushFace face = new BrushFace(reader, this, this.Vertices);

                // Always add this face to the 'all faces' list
                this.AllFaces.Add(face);

                // Only add this face to the 'visible faces' list if it's not hidden
                if ((face.Flags & BrushFace.FaceFlags.Hidden) == BrushFace.FaceFlags.None)
                    this.VisibleFaces.Add(face);
            }
        }
    }
}
